package io.renren.modules.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.demo.dao.DhComplainInfoDao;
import io.renren.modules.demo.dto.DhComplainInfoDTO;
import io.renren.modules.demo.entity.DhComplainInfoEntity;
import io.renren.modules.demo.service.DhComplainInfoService;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2023-03-25
 */
@Service
public class DhComplainInfoServiceImpl extends CrudServiceImpl<DhComplainInfoDao, DhComplainInfoEntity, DhComplainInfoDTO> implements DhComplainInfoService {

    @Autowired
    private DhComplainInfoDao dhComplainInfoDao;

    @Override
    public QueryWrapper<DhComplainInfoEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<DhComplainInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    /**
     * 用户发起投诉
     * @param dto
     */
    @Override
    @Transactional
    public void newComplain(DhComplainInfoDTO dto) {
        int complainId = ((int) ((Math.random() * 9 + 1) * 10000)) + 1000000;
        DhComplainInfoEntity dhComplainInfo = new DhComplainInfoEntity();
        dhComplainInfo.setId(complainId);
        dhComplainInfo.setUserId(dto.getUserId());
        dhComplainInfo.setComplainTime(dto.getComplainTime());
        dhComplainInfo.setProblem(dto.getProblem());
        dhComplainInfo.setDisposeStatus(0);
        dhComplainInfoDao.insert(dhComplainInfo);
    }

    /**
     * 确认处理投诉
     * @param id
     */
    @Override
    @Transactional
    public void disposeComplain(Long id) {
        DhComplainInfoEntity dhComplainInfoEntity = dhComplainInfoDao.selectById(id);
        dhComplainInfoEntity.setDisposeStatus(1);
        LambdaQueryWrapper<DhComplainInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select().eq(DhComplainInfoEntity::getId,id);
        dhComplainInfoDao.update(dhComplainInfoEntity,queryWrapper);
    }

    @Override
    public List<Integer> setComplainDisposeProportionPieData() {
        Integer disposeStatusIsOne = dhComplainInfoDao.getDisposeStatusIsOne();
        Integer disposeStatusIsZero = dhComplainInfoDao.getDisposeStatusIsZero();
        List<Integer> list = new ArrayList<>();
        list.add(disposeStatusIsOne);
        list.add(disposeStatusIsZero);
        return list;
    }
}